package com.example.administrator.zhongyichou.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.JoinActionBean;
import com.example.administrator.zhongyichou.utils.IdCard;
import com.example.administrator.zhongyichou.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/11/23.
 * 描述：
 */

public class JoinActionRecyclerAdapter extends RecyclerView.Adapter<JoinActionRecyclerAdapter.ViewHolder> {

    private Context context;
    private List<JoinActionBean> list;
    private OnDeleteClick onDeleteClick;

    public JoinActionRecyclerAdapter(Context context, List<JoinActionBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.join_action_recycler_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        //        强行关闭复用,以解决edittext错乱
        holder.setIsRecyclable(false);
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDeleteClick.onDeleteClick(position);
            }
        });
        holder.txtSort.setText("第" + (position + 1) + "位");
        holder.editName.addTextChangedListener(new TextChange(0, position, holder.txtType));
        holder.editName.setText(list.get(position).name);
        holder.editId.addTextChangedListener(new TextChange(1, position, holder.txtType));
        holder.editId.setText(list.get(position).cardID);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_sort)
        TextView txtSort;
        @BindView(R.id.txt_type)
        TextView txtType;
        @BindView(R.id.btn_delete)
        TextView btnDelete;
        @BindView(R.id.edit_name)
        EditText editName;
        @BindView(R.id.edit_id)
        EditText editId;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnDeleteClick {
        void onDeleteClick(int position);
    }

    public void setOnDeleteClick(OnDeleteClick onDeleteClick) {
        this.onDeleteClick = onDeleteClick;
    }

    class TextChange implements TextWatcher {
        /**
         * type==0  是姓名
         */
        int type, position;
        TextView textView;

        public TextChange(int type, int position, TextView textView) {
            this.type = type;
            this.position = position;
            this.textView = textView;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (type == 0) {
                list.get(position).name = s.toString();
            } else {
                list.get(position).cardID = s.toString();
                if (s.toString().length() == 18) {
                    textView.setText(getType(s.toString()));
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }


    private String getType(String idCard) {
        int age = IdCard.getAgeByIdCard(idCard);
        if (age < 18) {
            return "少年大病互助行动";
        } else if (age < 51) {
            return "中青年大病互助行动";
        } else if (age < 66) {
            return "老年大病互助行动";
        }
        return "-1";
    }

}
